package com.whzm.service.Impl;
import com.whzm.mapper.MemberInfoMapper;
import com.whzm.mapper.MemberResourcesMapper;
import com.whzm.pojo.MemberInfo;
import com.whzm.pojo.Resources;
import com.whzm.pojo.vo.ResourcesListVo;
import com.whzm.service.MemberResources;
import com.whzm.util.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class MemberResourcesImpl implements MemberResources {
    @Autowired
    MemberResourcesMapper memberResourcesMapper;
    @Autowired
    MemberInfoMapper memberInfoMapper;

    /**
 * 判断该用户是否是会员，且是否有效，若有效则可以查看特权信息*/
    @Override
    @Transactional(readOnly = true)
    public ResponseEntity getResourcesByMember(Resources resources, String userId) {

        MemberInfo memberInfo=memberInfoMapper.getMemberInfoByUserId(userId);
        //判断该用户是否是会员，且是否有效
        if(!StringUtils.isEmpty(memberInfo)&&!StringUtils.isEmpty(resources)){

            //判断是否有效,有效则返回查询数据
            if(memberInfo.getStatus().equals("1")){
                Integer page=resources.getPage();
                Integer limit=resources.getLimit();
                //执行查询
                List<ResourcesListVo> lists=memberResourcesMapper.getResourcesByManage(resources);
                //手动分页，将page，limit转换成list索引值
                Integer count=lists.size();
                Integer start=(page-1)*(limit);
                Integer end=(page-1)*(limit)+limit;
                if(count<end){
                    end=count;
                }
                //获取子集合
                List<ResourcesListVo> sublist=lists.subList(start,end);
                //数据封装
                return ResponseEntity.pageSuccess(1,"ok",sublist,count);
            }
        }
        return ResponseEntity.pageSuccess(0,"非会员无法查看该信息",null,0);
    }


}
